package com.bailian.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bailian.common.annotation.Log;
import com.bailian.common.core.controller.BaseController;
import com.bailian.common.core.domain.AjaxResult;
import com.bailian.common.enums.BusinessType;
import com.bailian.system.domain.BlWarehouse;
import com.bailian.system.domain.vo.BLWareHouseVo;
import com.bailian.system.service.IBlWarehouseService;
import com.bailian.common.utils.poi.ExcelUtil;
import com.bailian.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;
import com.bailian.system.domain.BlWarehouseType;

/**
 * 仓库基础信息Controller
 * 
 * @author bailian
 * @date 2025-08-04
 */
@RestController
@RequestMapping("/system/warehouse")
public class BlWarehouseController extends BaseController
{
    @Autowired
    private IBlWarehouseService blWarehouseService;

    /**
     * 查询仓库基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlWarehouse blWarehouse)
    {
        startPage();
        List<BLWareHouseVo> list = blWarehouseService.selectBlWarehouseVoList(blWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:export')")
    @Log(title = "仓库基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlWarehouse blWarehouse)
    {
        List<BLWareHouseVo> list = blWarehouseService.selectBlWarehouseVoList(blWarehouse);
        ExcelUtil<BLWareHouseVo> util = new ExcelUtil<BLWareHouseVo>(BLWareHouseVo.class);
        util.exportExcel(response, list, "仓库基础信息数据");
    }

    /**
     * 获取仓库基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    @GetMapping(value = "/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(blWarehouseService.selectBlWarehouseVoByWarehouseId(warehouseId));
    }

    /**
     * 调试：获取仓库基础信息（不包含关联查询）
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    @GetMapping(value = "/{warehouseId}/basic")
    public AjaxResult getBasicInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(blWarehouseService.selectWarehouseBasicInfo(warehouseId));
    }

    /**
     * 调试：获取仓库类型信息
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    @GetMapping(value = "/type/{typeId}")
    public AjaxResult getTypeInfo(@PathVariable("typeId") Long typeId)
    {
        return success(blWarehouseService.selectWarehouseTypeInfo(typeId));
    }

    /**
     * 调试：测试仓库类型查询（包含详细日志）
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    @GetMapping(value = "/debug/type/{typeId}")
    public AjaxResult debugTypeInfo(@PathVariable("typeId") Long typeId)
    {
        System.out.println("=== 开始调试仓库类型查询 ===");
        System.out.println("请求的类型ID: " + typeId);
        
        BlWarehouseType result = blWarehouseService.selectWarehouseTypeInfo(typeId);
        
        System.out.println("最终查询结果: " + result);
        System.out.println("=== 调试结束 ===");
        
        return success(result);
    }

    /**
     * 新增仓库基础信息
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:add')")
    @Log(title = "仓库基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlWarehouse blWarehouse,
                          @RequestParam(value = "tagIds", required = false) List<Long> tagIds)
    {
        int rows = blWarehouseService.insertBlWarehouse(blWarehouse);
        if (rows > 0 && blWarehouse.getWarehouseId() != null)
        {
            if (blWarehouse.getWarehouseTags() != null) {
                tagIds = blWarehouse.getWarehouseTags().stream().map(item->{return item.getTagId();}).collect(Collectors.toList());;
            }
            blWarehouseService.replaceWarehouseTags(blWarehouse.getWarehouseId(), tagIds);
        }
        return toAjax(rows);
    }

    /**
     * 修改仓库基础信息
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:edit')")
    @Log(title = "仓库基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlWarehouse blWarehouse,
                           @RequestParam(value = "tagIds", required = false) List<Long> tagIds)
    {
        int rows = blWarehouseService.updateBlWarehouse(blWarehouse);
        if (rows > 0 && blWarehouse.getWarehouseId() != null)
        {
            if (blWarehouse.getWarehouseTags() != null) {
                tagIds = blWarehouse.getWarehouseTags().stream().map(item->{return item.getTagId();}).collect(Collectors.toList());
            }
            blWarehouseService.replaceWarehouseTags(blWarehouse.getWarehouseId(), tagIds);
        }
        return toAjax(rows);
    }

    /**
     * 删除仓库基础信息
     */
    @PreAuthorize("@ss.hasPermi('system:warehouse:remove')")
    @Log(title = "仓库基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        return toAjax(blWarehouseService.deleteBlWarehouseByWarehouseIds(warehouseIds));
    }
}
